package com.kuqi.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.kuqi.BookInfoActivity;
import com.kuqi.R;
import com.kuqi.entitiy.Book;
import com.kuqi.utils.UrlTool;
import java.util.List;


public class BookStoreGridAdapter extends BaseAdapter {

    // 容器
    private Context mContext;
    // 信息列表
    private List<Book> dataList;


    // 构造
    public BookStoreGridAdapter(Context mContext, List<Book> dataList){
        this.mContext = mContext;
        this.dataList = dataList;
    }


    @Override
    public int getCount() {
        return dataList.size();
    }

    @Override
    public Book getItem(int position) {
        return dataList == null ? null : dataList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder mHolder;
        Book bookItem = getItem(position);
        if(convertView == null){
            convertView = LayoutInflater.from(mContext).inflate(R.layout.store_book_item, null);
            mHolder = new ViewHolder();
            mHolder.recommend_image = convertView.findViewById(R.id.iv_book_img);
            mHolder.recommend_title = convertView.findViewById(R.id.tv_book_name);
            convertView.setTag(mHolder);
        }else{
            mHolder = (ViewHolder) convertView.getTag();
        }

        Glide.with(mContext).load(UrlTool.book_base + bookItem.getThumbnail()).into(mHolder.recommend_image);
        mHolder.recommend_title.setText(bookItem.getBookName());

        // 为内容对象(视频item)添加点击事件
        convertView.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // 获取点击的视频所在item位置的视频实例信息
                Book book_info = dataList.get(position);
                // 携带视频参数跳转
                Intent intent = new Intent(mContext, BookInfoActivity.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable("book_info", book_info);
                intent.putExtras(bundle);
                mContext.startActivity(intent);
            }
        });

        return convertView;
    }


    // 用于临时缓存用于展现的数据
    private static class ViewHolder{
        private ImageView recommend_image;
        private TextView recommend_title;
    }
}
